package com.xzy.mybatis.cipher.support.registry;

import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement;
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement;
import com.xzy.mybatis.cipher.support.enums.ParseStrategyEnum;
import com.xzy.mybatis.cipher.support.strategy.parse.ParseStrategy;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author xzy.xiao
 * @since 2025/1/14  16:31
 */
public class SqlParseStrategyRegistry {

    private final Map<String, ParseStrategy> PARSE_STRATEGY_MAP = new ConcurrentHashMap<>(8);

    public void registerStrategy(String name, ParseStrategy parseStrategy) {
        PARSE_STRATEGY_MAP.put(name, parseStrategy);
    }

    public ParseStrategy getStrategy(SQLStatement sqlStatement) {
        if (sqlStatement instanceof SQLInsertStatement) {
            return PARSE_STRATEGY_MAP.get(ParseStrategyEnum.INSERT.getCode());
        } else if (sqlStatement instanceof SQLDeleteStatement) {
            return PARSE_STRATEGY_MAP.get(ParseStrategyEnum.DELETE.getCode());
        } else if (sqlStatement instanceof SQLUpdateStatement) {
            return PARSE_STRATEGY_MAP.get(ParseStrategyEnum.UPDATE.getCode());
        } else if (sqlStatement instanceof SQLSelectStatement) {
            return PARSE_STRATEGY_MAP.get(ParseStrategyEnum.SELECT.getCode());
        }
        return null;
    }

}
